/*
 * Copyright 2004-2011 Paolo Conte
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jellylab.ojoverse;

import java.io.Serializable;
import org.jellylab.ojoverse.types.Fieldtypes;
import org.jellylab.ojoverse.types.Keytypes;
import org.jellylab.ojoverse.types.BaseTypes.TypeStruct;

public class Column implements Serializable {
    private static final long serialVersionUID = 12357111317192329L;
    protected String name = "";
    protected String dbname = "";
    protected int group;
    protected int keytype;
    protected int fieldtype;
    protected TypeStruct datatype;

    public Column(String name, TypeStruct datatype) {
        this(name, datatype, 0);
    }

    public Column(String name, TypeStruct datatype, int keytype) {
        this(name, name, 0, datatype, keytype, 0);
    }

    public Column(String name, String dbname, int group, TypeStruct datatype) {
        this(name, dbname, group, datatype, 0, 0);
    }
    
    public Column(String name, String dbname, int group, TypeStruct datatype, int keytype, int fieldtype) {
        this.name = name;
        this.dbname = dbname;
        this.group = group;
        this.datatype = datatype;
        this.keytype = keytype;
        this.fieldtype = fieldtype;
    }

    public boolean equals(Column col) {
        return (dbname.equals(col.dbname)) && (name.equals(col.name)) && (datatype == col.datatype)
                && (keytype == col.keytype) && (fieldtype == col.fieldtype) && (datatype.getLenght() == col.datatype.getLenght());
    }

    public String toString() {
        return "Name: " + dbname + ", datatype:" + datatype + ", keytype:" + keytype + ", fieldtype:" + fieldtype + " lenght:" + datatype.getLenght();
    }

    public TypeStruct getDatatype() {
        return datatype;
    }

    public int getKeytype() {
        return keytype;
    }

    public int getFieldtype() {
        return fieldtype;
    }

    public String getName() {
        return name;
    }

    public String getDBName() {
        return dbname;
    }
    
    public int getGroup() {
        return group;
    }

    public boolean isKeytype(int coltype, int keytype) {
        return Keytypes.isKeytype(coltype, keytype);
    }

    public boolean isFieldtype(int coltype, int fieldtype) {
        return Fieldtypes.isFieldtype(coltype, fieldtype);
    }

    public boolean isUnique() {
        return (keytype & Keytypes.UNIQUE) > 0;
    }

    public boolean isPrimaryKey() {
        return (keytype & Keytypes.PRIMARYKEY) > 0;
    }

    public boolean isForeignKey() {
        return (keytype & Keytypes.FOREIGNKEY) > 0;
    }

    public boolean isDescription() {
        return (keytype & Keytypes.DESCRIPTION) > 0;
    }

    public boolean isFilter() {
        return (keytype & Keytypes.FILTER) > 0;
    }

    public boolean isPassword() {
        return (keytype & Fieldtypes.PASSWORD) > 0;
    }

    public boolean isFlag() {
        return (keytype & Fieldtypes.FLAG) > 0;
    }

    public boolean isCheck() {
        return (keytype & Fieldtypes.CHECK) > 0;
    }
}
